// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

package cmd

import (
	"fmt"
	"os"

	"github.com/kumose/kmopt/pkg/localdata"
	"github.com/spf13/cobra"
)

var envList = []string{
	localdata.EnvNameHome,
	localdata.EnvNameSSHPassPrompt,
	localdata.EnvNameSSHPath,
	localdata.EnvNameSCPPath,
	localdata.EnvNameKeepSourceTarget,
	localdata.EnvNameMirrorSyncScript,
	localdata.EnvNameLogPath,
	localdata.EnvNameDebug,
}

func newEnvCmd() *cobra.Command {
	cmd := &cobra.Command{
		Use:   "env [name1...N]",
		Short: "Show the list of system environment variable that related to KMOPT",
		RunE: func(cmd *cobra.Command, args []string) error {
			if len(args) == 0 {
				showEnvList(true, envList...)
				return nil
			}
			showEnvList(false, args...)
			return nil
		},
	}

	return cmd
}

func showEnvList(withKey bool, names ...string) {
	for _, name := range names {
		if withKey {
			fmt.Printf("%s=\"%s\"\n", name, os.Getenv(name))
		} else {
			fmt.Printf("%s\n", os.Getenv(name))
		}
	}
}
